package compoundDB.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;

public class Molecule implements Serializable {
	private List<Atom> m_atoms;
	private List<Bond> m_bonds;
	
	public Molecule(List<Atom> atoms, List<Bond> bonds) {
		m_atoms = atoms;
		m_bonds = bonds;
	}
	
	public List<Atom> getAtoms() {
		return m_atoms;
	}
	
	public List<Bond> getBonds() {
		return m_bonds;
	}
	
	
	/*
	 * Note: I'd prefer this stuff to be in the database itself
	 *  - I'm just putting it here so I don't mess up the db code
	 * ~ James
	 */
	public static byte[] serialize(Molecule m) throws IOException {
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		ObjectOutputStream o = new ObjectOutputStream(b);
		o.writeObject(m);
		
		o.close();
		
		return b.toByteArray();
	}
	
	public static Molecule deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
		ByteArrayInputStream b = new ByteArrayInputStream(bytes);
		ObjectInputStream o = new ObjectInputStream(b);
		Molecule m = (Molecule)o.readObject();
		
		o.close();
		
		return m;
	}
}
